#include "native/registry.h"

namespace jvm {
	namespace native {

        void EmptyNativeMethod(std::shared_ptr<rtda::Frame>)
        {
            // do nothing
        }

		std::unordered_map<std::string, MethodFunc> Registry::g_registry;
		//EmptyNativeMethod Registry::g_emptyNativeMethod;
        MethodFunc Registry::g_pEmptyNativeMethod = EmptyNativeMethod;

		//void EmptyNativeMethod::operator()(std::shared_ptr<rtda::Frame> frame) {
		//	// do nothing
		//}

        void Registry::registerMethod(
            const std::string& className,
            const std::string& methodName,
            const std::string& methodDescriptor,
            MethodFunc method
        )
        {
            std::string key = className + "~" + methodName + "~" + methodDescriptor;
            g_registry[key] = method;
        }

        MethodFunc Registry::findNativeMethod(
            const std::string& className,
            const std::string& methodName,
            const std::string& methodDescriptor
        )
        {
            std::string key = className + "~" + methodName + "~" + methodDescriptor;
            auto it = g_registry.find(key);
            if (it != g_registry.end()) {
                return it->second;
            }
            if (methodDescriptor == "()V" && methodName == "registerNatives") {
                return g_pEmptyNativeMethod;
            }
            return MethodFunc();
        }



	} // namespace native
} // namespace jvm